/*
 * This file is part of the Try! Engine project.
 *
 * Copyright 2010-2011 Emanuele Bertoldi. All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * You should have received a copy of the modified BSD License along with this
 * program. If not, see <http://www.opensource.org/licenses/bsd-license.php>
 */

#include "nodes.h"
#include <agent.h>
#include <model3d.h>

using namespace Try;

#define STEERING_MAX_SPEED 2
#define ANIM_SPEED_UP 30

WaitForTarget::WaitForTarget(SceneNode* target, float radius, BehaviorTree* parent)
    :   Action(parent),
        m_target(target),
        m_sqrRadius(radius * radius)
{
}

WaitForTarget::~WaitForTarget()
{
}

void WaitForTarget::onMessage(const Message& msg, Object* sender, Property* property)
{
    this->setStatus(BehaviorNode::Running);

    if (property && property->is(Try::Agent::name) && msg.is(Try::Agent::Msg::think))
    {
        float dt = msg.param<float>();

        Try::Model3D* model = sender->property<Try::Model3D>();

        if (model)
        {
            Vector3 pos = model->position();
            Vector3 target_pos = m_target->transform().position;

            if (pos.sqrDistance(target_pos) <= m_sqrRadius)
            {
                this->setStatus(BehaviorNode::Success);
                return;
            }

            model->stopAnimation(1);
            model->stopAnimation(2);
            model->runAnimation(0, 1, ANIM_SPEED_UP);
            model->lookAt(target_pos);
        }
    }
}

Approach::Approach(SceneNode* target, float radius, BehaviorTree* parent)
    :   Steering(parent, STEERING_MAX_SPEED),
        m_target(target),
        m_sqrRadius(radius * radius)
{
}

Approach::~Approach()
{
}

void Approach::onMessage(const Message& msg, Object* sender, Property* property)
{
    this->setStatus(BehaviorNode::Running);

    if (property && property->is(Try::Agent::name) && msg.is(Try::Agent::Msg::think))
    {
        float dt = msg.param<float>();

        Try::Model3D* model = sender->property<Try::Model3D>();

        if (model)
        {
            Vector3 pos = model->position();
            Vector3 target_pos = m_target->transform().position;

            if (pos.sqrDistance(target_pos) <= m_sqrRadius)
            {
                this->setStatus(BehaviorNode::Success);
                return;
            }

            Vector3 vel(this->seek(pos, target_pos));
            vel.y = 0;

            if (vel.length() > 0)
            {
                model->stopAnimation(0);
                model->stopAnimation(2);
                model->runAnimation(1, 1, ANIM_SPEED_UP);
                model->translate(vel * dt);
                model->lookAt(target_pos);
            }
        }
    }
}

Flee::Flee(SceneNode* target, BehaviorTree* parent)
    :   Steering(parent, STEERING_MAX_SPEED),
        m_target(target)
{
}

Flee::~Flee()
{
}

void Flee::onMessage(const Message& msg, Object* sender, Property* property)
{
    this->setStatus(BehaviorNode::Running);

    if (property && property->is(Try::Agent::name) && msg.is(Try::Agent::Msg::think))
    {
        float dt = msg.param<float>();

        Try::Model3D* model = sender->property<Try::Model3D>();

        if (model)
        {
            Vector3 pos = model->position();
            Vector3 target_pos = m_target->transform().position;

            Vector3 vel(this->flee(pos, target_pos));
            vel.y = 0;

            if (vel.length() > 0)
            {
                model->stopAnimation(0);
                model->stopAnimation(2);
                model->runAnimation(1, 1, ANIM_SPEED_UP);
                model->translate(vel * dt);
                model->lookAt(target_pos);
            }
        }
    }

    this->setStatus(BehaviorNode::Success);
}

Attack::Attack(SceneNode* target, BehaviorTree* parent)
    :   Action(parent),
        m_target(target)
{
}

Attack::~Attack()
{
}

void Attack::onMessage(const Message& msg, Object* sender, Property* property)
{
    this->setStatus(BehaviorNode::Running);

    if (property && property->is(Try::Agent::name) && msg.is(Try::Agent::Msg::think))
    {
        float dt = msg.param<float>();

        Try::Model3D* model = sender->property<Try::Model3D>();

        if (model)
        {
            Vector3 target_pos = m_target->transform().position;

            model->stopAnimation(0);
            model->stopAnimation(1);
            model->runAnimation(2, 1, ANIM_SPEED_UP);
            model->lookAt(target_pos);
        }
    }

    this->setStatus(BehaviorNode::Success);
}
